package controller

import (
	"fmt"
	"gin_demo/dao"
	"gin_demo/service"
	"gin_demo/utils"
	"github.com/gin-gonic/gin"
	"net/http"
)

func (uc *UserHandler) UserLogin(c *gin.Context) {
	var requestData dao.User
	if err := c.ShouldBindJSON(&requestData); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"success": false,
			"token":   "",
			"error":   err.Error(),
		})
		return
	}

	if requestData.Username == "" || requestData.Password == "" {
		c.JSON(http.StatusBadRequest, gin.H{
			"success": false,
			"token":   "",
			"error":   "用户或密码为空",
		})
		return
	}

	_, err := service.Us.GetUserByUserNameAndPassword(&requestData)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"success": false,
			"token":   "",
			"error":   err.Error(),
		})
		return
	}

	token, err := utils.Jwt.CreateToken(requestData.Username)

	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"success": false,
			"token":   "",
			"error":   err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"token":   token,
		"error":   "",
	})
}

func (uc *UserHandler) UserReg(c *gin.Context) {
	var requestData dao.User
	if err := c.ShouldBindJSON(&requestData); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"success": false,
			"token":   "",
			"error":   err.Error(),
		})
		return
	}

	if requestData.Username == "" || requestData.Password == "" {
		c.JSON(http.StatusBadRequest, gin.H{
			"success": false,
			"token":   "",
			"error":   "用户或密码为空",
		})
		return
	}

	ok := service.Us.RegUser(&requestData)

	if !ok {
		c.JSON(http.StatusOK, gin.H{
			"success": false,
			"token":   "",
			"error":   "注册失败",
		})
		return
	}

	token, err := utils.Jwt.CreateToken(requestData.Username)

	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"success": false,
			"token":   "",
			"error":   err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"token":   token,
		"error":   "",
	})
}

func (uc *UserHandler) GetAllUser(c *gin.Context) {

	userList, err1 := service.Us.QueryAllUser()
	token, err2 := utils.Jwt.CreateToken(c.Param("username"))
	if err1 != nil || err2 != nil {
		c.JSON(http.StatusOK, gin.H{
			"success": false,
			"users":   nil,
			"token":   "",
		})
		return
	}
	fmt.Println(c.Param("username"))
	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"users":   userList,
		"token":   token,
	})
}
func (uc *UserHandler) DelUserByUsername(c *gin.Context) {
	var requestData dao.User
	if err := c.ShouldBindJSON(&requestData); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"success": false,
			"token":   "",
			"error":   err.Error(),
		})
		return
	}

	if requestData.Username == "" {
		c.JSON(http.StatusBadRequest, gin.H{
			"success": false,
			"token":   "",
			"error":   "用户名为空",
		})
		return
	}

	ok := service.Us.DelUserByUsername(&requestData)

	if !ok {
		c.JSON(http.StatusOK, gin.H{
			"success": false,
			"token":   "",
			"error":   "删除失败",
		})
		return
	}

	token, err := utils.Jwt.CreateToken(requestData.Username)

	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"success": false,
			"token":   "",
			"error":   err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"token":   token,
		"error":   "",
	})
}

func (uc *UserHandler) UpdatePasswordByUsername(c *gin.Context) {
	var requestData dao.User
	if err := c.ShouldBindJSON(&requestData); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"success": false,
			"token":   "",
			"error":   err.Error(),
		})
		return
	}

	if requestData.Username == "" {
		c.JSON(http.StatusBadRequest, gin.H{
			"success": false,
			"token":   "",
			"error":   "用户名为空",
		})
		return
	}

	ok := service.Us.UpdatePasswordByUsername(&requestData)

	if !ok {
		c.JSON(http.StatusOK, gin.H{
			"success": false,
			"token":   "",
			"error":   "更新失败",
		})
		return
	}

	token, err := utils.Jwt.CreateToken(requestData.Username)

	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"success": false,
			"token":   "",
			"error":   err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"token":   token,
		"error":   "",
	})
}
